import { defineStore } from 'pinia'

interface IOTState {
  IOTstatus: boolean;
}

const useIOTStore = defineStore('iot', {
  state(): IOTState {
    return {
      IOTstatus: false
    }
  },
  
  getters: {
    // 获取当前的 IOT 状态
    getIOTStatus(state): boolean {
      return state.IOTstatus;
    }
  },

  actions: {
    // 更新 IOT 状态，并将其保存到 localStorage
    updateIOTStatus(status: boolean) {
      this.IOTstatus = status;
      localStorage.setItem('IOTstatus', status.toString());
    },
    
    // 从 localStorage 恢复 IOT 状态
    loadIOTStatus() {
      const savedStatus = localStorage.getItem('IOTstatus');
      if (savedStatus) {
        this.IOTstatus = savedStatus === 'true';
      }
    }
  }
});

export const iotStore = useIOTStore;
